﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Common;
using Dao;
using Domain.Common;
using Domain.Map;
using Domain.Officers;
using Microsoft.Xna.Framework;

namespace Domain
{
    public partial class Building
    {
        #region 属性
        private readonly static log4net.ILog Logger = log4net.LogManager.GetLogger("Building");
        private Random rd = new Random(System.DateTime.Now.Millisecond);
        public static Dictionary<short, Building> Buildings = new Dictionary<short, Building>();
        public short Id { get; set; }
        public string NumberName { get; set; }
        public string Name { get; set; }
        public string CityTypeId { get; set; }
        /// <summary>
        /// 城池规模等级[0,5),[5,13),[13,...)
        /// </summary>
        public int CityLevel { get; set; }
        public bool HasEmper { get; set; }
        public short? ForceId { get; set; }
        /// <summary>
        /// 建筑物坐标位置【列，行】，整个地图划分成300X300个最小分割单位
        /// </summary>
        public Point Position;
        /// <summary>
        /// 人口
        /// </summary>
        public int Population { get; set; }
        /// <summary>
        /// 资金，单位钱（五铢钱），1斛粮食=500钱，通货膨胀最高1斛粮食=2000钱
        /// </summary>
        public int Fund { get; set; }
        /// <summary>
        /// 粮食,单位斛，1斛=1石=10斗=120斤，一兵一天吃一斤，1万兵，一天要吃83斛，半年就要吃15166斛了
        /// </summary>
        public int Food { get; set; }
        /// <summary>
        /// 农业
        /// </summary>
        public int Agriculture { get; set; }
        /// <summary>
        /// 商业
        /// </summary>
        public int Commerce { get; set; }
        /// <summary>
        /// 技术
        /// </summary>
        public int Technology { get; set; }
        /// <summary>
        /// 文化
        /// </summary>
        public int Culture { get; set; }
        /// <summary>
        /// 治安
        /// </summary>
        public int PublicSecurity { get; set; }
        /// <summary>
        /// 民心
        /// </summary>
        public int PopularFeelings { get; set; }
        /// <summary>
        /// 是否有港口
        /// </summary>
        public bool HasPort;
        /// <summary>
        /// 城墙耐久
        /// </summary>
        public int Endurance { get; set; }
        /// <summary>
        /// 固定最大耐久，非发展型城池专用（关隘，港口）
        /// </summary>
        private int _fixEnduranceMax;
        /// <summary>
        /// 城池本次扩建当前已完成建设值
        /// </summary>
        public int CurrentExtendBuilding;
        /// <summary>
        /// 是否正在扩建城墙
        /// </summary>
        public bool IsExtendingBuilding;
        /// <summary>
        /// 直接连接的临近城池Ids
        /// </summary>
        public string LinkedBuildingsIds;
        /// <summary>
        /// 兵役人口，默认是每5个人口中有一个可以服兵役
        /// </summary>
        public int SoldierPopulation;
        /// <summary>
        /// 兵力
        /// </summary>
        public int SoldierCount;
        /// <summary>
        /// 当前城池等级
        /// </summary>
        private short _currentLevel;
        /// <summary>
        /// 城池最大可升级等级
        /// </summary>
        private short _maxLevel;
        /// <summary>
        /// 上次粮食收获时间
        /// </summary>
        public DateTime LastHarvest=DateTime.MinValue;
        /// <summary>
        /// 上次发月俸时间
        /// </summary>
        public DateTime LastPayDay = DateTime.MinValue;

        public BuildingLevel CurrentBuildingLevel;

        public BuildingLevel MaxBuildingLevel;
        /// <summary>
        /// 战略类型
        /// </summary>
        public StrategyType StrategyType;
        /// <summary>
        /// / 直接连接的临近城池
        /// </summary>
        public List<Building> NearlyBuildings = new List<Building>();
        /// <summary>
        /// 长官
        /// </summary>
        public Officer Master;
        /// <summary>
        /// 所属势力
        /// </summary>
        public Force Force { get; set; }
        /// <summary>
        /// 当前归属城池的武将
        /// </summary>
        public List<Officer> OwnedOfficers = new List<Officer>();
        /// <summary>
        /// 当前在城池武将(包括自己的、其他势力的、在野的)
        /// </summary>
        public List<Officer> InBuildingOfficers = new List<Officer>();
        /// <summary>
        /// 各类工作分配的资金
        /// </summary>
        public Dictionary<WorkType, int> FundAllocation = new Dictionary<WorkType, int>();
        /// <summary>
        /// 城池可以售卖或藏身的商品宝物
        /// </summary>
        public List<Treasure> HasTreasures=new List<Treasure>();
        #endregion
        #region  初始化数据
        public static void Load()
        {
            if (Buildings.Count > 0)
                return;
            DataTable tb = BuildingDao.SelectAllBuilding("194_QXGJ.s3db");
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                Building s = new Building();
                s.Id = Convert.ToInt16(dr["Id"]);
                if (dr["ForceId"] != DBNull.Value)
                    s.ForceId = Convert.ToInt16(dr["ForceId"]);
                s.NumberName = dr["NumberName"].ToString();
                s.Name = dr["Name"].ToString();
                s.HasEmper = Convert.ToBoolean(dr["HasEmper"]);
                s.CityTypeId = dr["CityTypeId"].ToString();
                string tempArea = dr["Position"].ToString();
                string[] tempArea2 = tempArea.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                s.Position = new Point(Convert.ToInt32(tempArea2[0]), Convert.ToInt32(tempArea2[1]));
                s.Population = Convert.ToInt32(dr["Population"]);
                s.Fund = Convert.ToInt32(dr["Fund"]);
                s.Food = Convert.ToInt32(dr["Food"]);
                s.Agriculture = Convert.ToInt32(dr["Agriculture"]);
                s.Commerce = Convert.ToInt32(dr["Commerce"]);
                s.Technology = Convert.ToInt32(dr["Technology"]);
                s.Culture = Convert.ToInt32(dr["Culture"]);
                s.PublicSecurity = Convert.ToInt32(dr["PublicSecurity"]);
                s.PopularFeelings = Convert.ToInt32(dr["PopularFeelings"]);
                s.Endurance = Convert.ToInt32(dr["Endurance"]);
                s._fixEnduranceMax = s.Endurance;
                s.LinkedBuildingsIds = dr["LinkedBuildings"].ToString();
                s.HasPort = Convert.ToBoolean(dr["HasPort"]);
                s.SoldierPopulation = Convert.ToInt32(dr["SoldierPopulation"]);
                s._currentLevel = Convert.ToInt16(dr["CurrentLevel"]);
                s._maxLevel = Convert.ToInt16(dr["MaxLevel"]);
                Buildings.Add(s.Id, s);
                MapUnit.AddThings(s);
            }
        }
        public static void Init()
        {
            foreach (KeyValuePair<short, Building> pair in Buildings)
            {
                if (pair.Value.ForceId != null)
                {
                    Force force = Force.Forces[(short)pair.Value.ForceId];
                    pair.Value.Force = force;
                    force.Buildings.Add(pair.Value);
                }
                //装配附近的城池
                string[] links = pair.Value.LinkedBuildingsIds.Split(",".ToCharArray(),
                    StringSplitOptions.RemoveEmptyEntries);
                foreach (var link in links)
                {
                    pair.Value.NearlyBuildings.Add(Buildings[Convert.ToInt16(link)]);
                }
                if (pair.Value._currentLevel != 0)
                    pair.Value.CurrentBuildingLevel = BuildingLevel.BuildingLevels[pair.Value._currentLevel];
                if (pair.Value._maxLevel != 0)
                    pair.Value.MaxBuildingLevel = BuildingLevel.BuildingLevels[pair.Value._maxLevel];
               
            }
            //装配商品宝物
            DataTable tb = BuildingHasTreasureDao.SelectAllBuildingTreasure("194_QXGJ.s3db");
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                short buildingId = Convert.ToInt16(dr["BuildingId"]);
                int treasureId = Convert.ToInt16(dr["TreasureId"]);
                Building building = Buildings[buildingId];
                Treasure treasure = Treasure.Treasures[treasureId];
                building.HasTreasures.Add(treasure);
            }
        }

        #endregion
    }
}
